package cn.dapeng.rpc.common;


import cn.dapeng.rpc.common.serial.SerializeFactory;
import cn.dapeng.rpc.common.util.MagicNumberUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.UUID;

@Getter
@Setter
public class RPCHeader implements Serializable {

    private long requestId = Math.abs(UUID.randomUUID().getLeastSignificantBits());

    private int requestCode;


    public static void main(String[] args) throws Exception {

        RPCHeader header = new RPCHeader();
        header.setRequestCode(100);

        byte[] h = SerializeFactory.serial(header, Constant.SERIAL_VERSION);
        //byte[] h = JDKSerializeCoder.serial(header);
        System.out.println(h.length);

        int magicNum = MagicNumberUtil.getMagic(Constant.VERSION, h.length, 0, Constant.SERIAL_VERSION, RequestTypeEnum.REQUEST.ordinal());


        MagicNumberUtil.decodec(magicNum);

        ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.directBuffer(1 + h.length);

        byteBuf.writeInt(magicNum);
        byteBuf.writeBytes(h);

        System.out.println(byteBuf.readInt());

        int len = MagicNumberUtil.getHeaderLen(magicNum);
        System.out.println(len);
        byte[] bytes = new byte[h.length];
        byteBuf.readBytes(bytes);


        RPCHeader deseria = SerializeFactory.deseria(bytes, RPCHeader.class, MagicNumberUtil.getSerialVersion(magicNum));
        System.out.println(deseria);


    }


}
